﻿using DevExpress.XtraEditors;
using System;
using System.Windows.Forms;
/// <summary>
/// 分页
/// </summary>
namespace MDK.Winform
{
    /// <summary>
    /// 分页
    /// </summary>
    public partial class ucPaging : XtraUserControl
    {
        /// <summary>
        /// 默认无参构造函数
        /// </summary>
        public ucPaging()
        {
            InitializeComponent();
            textPageNumber.ImeMode = ImeMode.Disable;
        }
        /// <summary>
        /// 页大小
        /// </summary>
        private int pageSize;
        /// <summary>
        /// 总记录数
        /// </summary>
        private int totalRecord;
        /// <summary>
        /// 总页数
        /// </summary>
        private int pageCount;
        /// <summary>
        /// 页码
        /// </summary>
        private int pageNumber;
        /// <summary>
        /// 翻页事件
        /// </summary>
        public event EventHandler FlipPageHandler;
        /// <summary>
        /// 页大小
        /// </summary>
        public int PageSize
        {
            get
            {
                return pageSize;
            }

            set
            {
                pageSize = value;
                if (this.IsHandleCreated && lbPageSize.IsHandleCreated)
                {
                    this.Invoke(new MethodInvoker(() =>
                    {
                        lbPageSize.Text = "每页" + pageSize + "条";
                    }));
                }
            }
        }
        /// <summary>
        /// 总记录数
        /// </summary>
        public int TotalRecord
        {
            get
            {
                return totalRecord;
            }

            set
            {
                totalRecord = value;
                if (totalRecord == 0 || pageSize == 0)
                {
                    PageCount = 0;
                }
                else
                {
                    PageCount = Convert.ToInt32(Math.Ceiling(totalRecord * 1.0 / pageSize));
                }
                if (this.IsHandleCreated && lbTotalRecord.IsHandleCreated)
                {
                    this.Invoke(new MethodInvoker(() =>
                    {
                        lbTotalRecord.Text = "共" + totalRecord + "条数据";
                    }));
                }
            }
        }
        /// <summary>
        /// 总页数
        /// </summary>
        public int PageCount
        {
            get
            {
                return pageCount;
            }

            set
            {
                pageCount = value;
                if (this.IsHandleCreated && lbPageCount.IsHandleCreated)
                {
                    this.Invoke(new MethodInvoker(() =>
                    {
                        lbPageCount.Text = "共" + pageCount + "页";
                    }));
                }
            }
        }
        /// <summary>
        /// 页码
        /// </summary>
        public int PageNumber
        {
            get
            {
                return pageNumber;
            }

            set
            {
                if (value < 1)
                {
                    pageNumber = 1;
                }
                else if (value > PageCount)
                {
                    pageNumber = PageCount;
                }
                else
                {
                    pageNumber = value;
                }
                if (this.IsHandleCreated && textPageNumber.IsHandleCreated && btnPrev.IsHandleCreated && btnNext.IsHandleCreated && btnFirst.IsHandleCreated && btnLast.IsHandleCreated)
                {
                    this.Invoke(new MethodInvoker(() =>
                    {
                        textPageNumber.Text = pageNumber.ToString();
                        if (pageNumber == 1 && pageNumber == PageCount)
                        {
                            btnPrev.Enabled = false;
                            btnNext.Enabled = false;
                            btnFirst.Enabled = false;
                            btnLast.Enabled = false;
                        }
                        else if (pageNumber == 1)
                        {
                            btnPrev.Enabled = false;
                            btnNext.Enabled = true;
                            btnFirst.Enabled = false;
                            btnLast.Enabled = true;
                        }
                        else if (pageNumber == PageCount)
                        {
                            btnPrev.Enabled = true;
                            btnNext.Enabled = false;
                            btnFirst.Enabled = true;
                            btnLast.Enabled = false;
                        }
                        else
                        {
                            btnPrev.Enabled = true;
                            btnNext.Enabled = true;
                            btnFirst.Enabled = true;
                            btnLast.Enabled = true;
                        }
                    }));
                }
            }
        }
        /// <summary>
        /// 第一页按钮点击事件
        /// </summary>
        private void btnFirst_Click(object sender, EventArgs e)
        {
            PageNumber = 1;
            FlipPageHandler?.Invoke(sender, e);
        }
        /// <summary>
        /// 前一页按钮点击事件
        /// </summary>
        private void btnPrev_Click(object sender, EventArgs e)
        {
            PageNumber -= 1;
            FlipPageHandler?.Invoke(sender, e);
        }
        /// <summary>
        /// 下一页按钮点击事件
        /// </summary>
        private void btnNext_Click(object sender, EventArgs e)
        {
            PageNumber += 1;
            FlipPageHandler?.Invoke(sender, e);
        }
        /// <summary>
        /// 最后一页按钮点击事件
        /// </summary>
        private void btnLast_Click(object sender, EventArgs e)
        {
            PageNumber = PageCount;
            FlipPageHandler?.Invoke(sender, e);
        }
        /// <summary>
        /// 页码框文本改变事件
        /// </summary>
        private void textPageNumber_KeyUp(object sender, KeyEventArgs e)
        {
            bool DKey = e.KeyCode == Keys.D0 || e.KeyCode == Keys.D1 || e.KeyCode == Keys.D2 || e.KeyCode == Keys.D3 || e.KeyCode == Keys.D4 || e.KeyCode == Keys.D5 || e.KeyCode == Keys.D6 || e.KeyCode == Keys.D7 || e.KeyCode == Keys.D8 || e.KeyCode == Keys.D9;
            bool NKey = e.KeyCode == Keys.NumPad0 || e.KeyCode == Keys.NumPad1 || e.KeyCode == Keys.NumPad2 || e.KeyCode == Keys.NumPad3 || e.KeyCode == Keys.NumPad4 || e.KeyCode == Keys.NumPad5 || e.KeyCode == Keys.NumPad6 || e.KeyCode == Keys.NumPad7 || e.KeyCode == Keys.NumPad8 || e.KeyCode == Keys.NumPad9;
            if (DKey || NKey)
            {
                if (!string.IsNullOrEmpty(textPageNumber.Text))
                {
                    int pageNumber;
                    if (int.TryParse(textPageNumber.Text, out pageNumber) && pageNumber >= 1 && pageNumber <= PageCount)
                    {
                        PageNumber = pageNumber;
                        FlipPageHandler?.Invoke(sender, e);
                        return;
                    }
                }
                textPageNumber.Text = PageNumber.ToString();
            }
        }
    }
}

